{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting pandapipes Networks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This tutorial shows how to plot a pandapipes network."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple Plotting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The simple plot function allows you to plot networks to get a fast visualisation. There is no need to gain a deep \n",
    "understanding of the plotting module.\n",
    "\n",
    "First of all, a simple network with genuine geodata is created. To get a better understanding of\n",
    "creating networks, follow the [Creating a simple network](https://github.com/e2nIEE/pandapipes/blob/develop/tutorials/creating_a_simple_network.ipynb) tutorial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import pandapipes as pp\n",
    "\n",
    "# create an empty network\n",
    "net = pp.create_empty_network(fluid=\"lgas\")\n",
    "\n",
    "# create network elements, such as junctions, external grid, pipes, valves, sinks and sources\n",
    "junction1 = pp.create_junction(net, pn_bar=1.05, tfluid_k=293.15, name=\"Connection to External Grid\", geodata=(0, 0))\n",
    "junction2 = pp.create_junction(net, pn_bar=1.05, tfluid_k=293.15, name=\"Junction 2\", geodata=(2, 0))\n",
    "junction3 = pp.create_junction(net, pn_bar=1.05, tfluid_k=293.15, name=\"Junction 3\", geodata=(7, 4))\n",
    "junction4 = pp.create_junction(net, pn_bar=1.05, tfluid_k=293.15, name=\"Junction 4\", geodata=(7, -4))\n",
    "junction5 = pp.create_junction(net, pn_bar=1.05, tfluid_k=293.15, name=\"Junction 5\", geodata=(5, 3))\n",
    "junction6 = pp.create_junction(net, pn_bar=1.05, tfluid_k=293.15, name=\"Junction 6\", geodata=(5, -3))\n",
    "\n",
    "ext_grid = pp.create_ext_grid(net, junction=junction1, p_bar=1.1, t_k=293.15, name=\"Grid Connection\")\n",
    "\n",
    "pipe1 = pp.create_pipe_from_parameters(net, from_junction=junction1, to_junction=junction2, length_km=10, diameter_m=0.3, name=\"Pipe 1\", geodata=[(0, 0), (2, 0)])\n",
    "pipe2 = pp.create_pipe_from_parameters(net, from_junction=junction2, to_junction=junction3, length_km=2, diameter_m=0.3, name=\"Pipe 2\", geodata=[(2, 0), (2, 4), (7, 4)])\n",
    "pipe3 = pp.create_pipe_from_parameters(net, from_junction=junction2, to_junction=junction4, length_km=2.5, diameter_m=0.3, name=\"Pipe 3\", geodata=[(2, 0), (2, -4), (7, -4)])\n",
    "pipe4 = pp.create_pipe_from_parameters(net, from_junction=junction3, to_junction=junction5, length_km=1, diameter_m=0.3, name=\"Pipe 4\", geodata=[(7, 4), (7, 3), (5, 3)])\n",
    "pipe5 = pp.create_pipe_from_parameters(net, from_junction=junction4, to_junction=junction6, length_km=1, diameter_m=0.3, name=\"Pipe 5\", geodata=[(7, -4), (7, -3), (5, -3)])\n",
    "\n",
    "valve = pp.create_valve(net, from_junction=junction5, to_junction=junction6, diameter_m=0.05, opened=True)\n",
    "\n",
    "sink = pp.create_sink(net, junction=junction4, mdot_kg_per_s=0.545, name=\"Sink 1\")\n",
    "\n",
    "source = pp.create_source(net, junction=junction3, mdot_kg_per_s=0.234)\n",
    "pp.pipeflow(net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.res_junction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The simple network contains the most common elements that are supported by the pandapipes format. \n",
    "\n",
    "<img src=\"pics/simple_network-1.png\">\n",
    "\n",
    "In comparison to the above image, the simple plot function shows the network as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "# import the plotting module\n",
    "import pandapipes.plotting as plot\n",
    "\n",
    "# plot network\n",
    "plot.simple_plot(net, plot_sinks=True, plot_sources=True, sink_size=4.0, source_size=4.0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting Collections"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Within the simple plot function, simple collections are generated automatically. For example a collection for all junctions, which are then plotted as red circles. \n",
    "\n",
    "However, the users can also define their own collections and plot them together. This allows for easy design modifications.\n",
    "\n",
    "##### What is a collection?\n",
    "A collection constist of an assemblage of different information about patchtype, colour, size and others. \n",
    "\n",
    "##### Patches\n",
    "Patches are pre-designed symbols. There exist individual patches for valves and sources, or for symbols in the shape of a circle, rectangle, etc. \n",
    "\n",
    "##### Why using collections?\n",
    "It is easier for the plotting module to sort certain elements in collections. This makes the plotting itself faster by reducing time and effort for calculations. Additionally you can control the layout of your plot individually by creating your own additional collections. \n",
    "\n",
    "##### Additional collections\n",
    "If you want to mark some of the elements of your network differently, you can add them to an individual collections. For example, you can add all junctions with a sink connection to a collection called *junction_sink_collection* and set the collection configurations to an orange circle. By using this functions you can easily organize the plot or create an individual plot. \n",
    "\n",
    "##### Element sizes\n",
    "The size of the elements corresponds with the size and the type of the plot. The size can be chosen manually or be fetched with the function *get_collection_sizes*."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To point out different elements, you can create additional collections for these elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create additional junction collections for junctions with sink connections and junctions with valve connections\n",
    "junction_sink_collection = plot.create_junction_collection(net, junctions=[3], patch_type=\"circle\", size=0.1, color=\"orange\", zorder=200)\n",
    "junction_source_collection = plot.create_junction_collection(net, junctions=[2], patch_type=\"circle\", size=0.1, color=\"green\", zorder=200)\n",
    "junction_valve_collection = plot.create_junction_collection(net, junctions=[4, 5], patch_type=\"rect\",size=0.1, color=\"red\", zorder=200)\n",
    "\n",
    "# create additional pipe collection\n",
    "pipe_collection = plot.create_pipe_collection(net, pipes=[3,4], linewidths=5., zorder=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, it is posssible to plot only the collections you designed individually. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot collections of junctions and pipes\n",
    "plot.draw_collections([junction_sink_collection, junction_source_collection, junction_valve_collection, pipe_collection],  figsize=(8,6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to plot your network including the additional collections you need to add them to the simple collections which are created automatically with the simple plot function. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a list of simple collections\n",
    "simple_collections = plot.create_simple_collections(net, as_dict=False)\n",
    "\n",
    "# add additional collections to the list\n",
    "simple_collections.append([junction_sink_collection, junction_source_collection, junction_valve_collection, pipe_collection])\n",
    "\n",
    "# plot list of all collections\n",
    "plot.draw_collections(simple_collections)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Keep in mind that additional collections can be layered above already existing collections. You need to set a high zorder for the elements you want to draw in front. \n",
    "\n",
    "As pandapipes is based on pandapower, more plotting tutorials with pandapower are available at https://www.pandapower.org/start/ ."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}